package com.zhiyume.controller;

import com.zhiyume.api.util.ApiClassifyEnum;
import com.zhiyume.dao.entity.TAdminSys;
import com.zhiyume.dao.entity.TCmsHome;
import com.zhiyume.service.CmsHomeService;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.shiro.AdminSecurityManager;
import com.zhiyume.vo.CommonReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wang on 18/1/18.
 */
@Controller
@RequestMapping("/cmsHome")
public class CmsHomeController {

    @Autowired
    private CmsHomeService cmsHomeService;

    /**
     * 获取所有分类的资讯列表
     * @param commonReqVO
     * @return
     */
    @RequestMapping(value="/getCmsHomeList")
    @ResponseBody
    public Object getCmsHomeList(CommonReqVO commonReqVO){
        List<TCmsHome> cms = cmsHomeService.getCmsHomeList(commonReqVO);
        int count = cmsHomeService.getCmsHomeCount(commonReqVO);
        Map result = new HashMap();
        result.put("list",cms);
        result.put("total",count);
        result.put("page",commonReqVO.getPage());
        return result;
    }

    @RequestMapping("/cmsHomeListPage")
    public ModelAndView CmsHomeListPage(){
        ModelAndView modelAndView = new ModelAndView();
        List navList = ApiClassifyEnum.getNavList();
        modelAndView.addObject("navList", navList);
        modelAndView.setViewName("page/cmsHome/cmsHomeList");
        return modelAndView;
    }

    @RequestMapping("/addCmsHome")
    @ResponseBody
    public Map addCmsHome(TCmsHome cmsHome){
        String result = "success";
        TAdminSys sysUser = AdminSecurityManager.getSessionTAdminSys();

        cmsHome.setCreateId(sysUser.getId());
        cmsHome.setUpdateId(sysUser.getId());
        cmsHome.setCreateTime(DateUtil.getNow());
        cmsHome.setUpdateTime(DateUtil.getNow());
        try {
            cmsHomeService.addCmsHome(cmsHome);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }


    @RequestMapping("/validCmsHome")
    @ResponseBody
    public Map validCmsHome(Long id){
        String result = "success";
        TAdminSys sysUser = AdminSecurityManager.getSessionTAdminSys();

        TCmsHome cmsHome = new TCmsHome();
        try {
            cmsHome = cmsHomeService.findCmsHome(id);
            cmsHome.setUpdateId(sysUser.getId());
            cmsHomeService.validCmsHome(cmsHome);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }

    @RequestMapping("/inValidCmsHome")
    @ResponseBody
    public Map inValidCmsHome(Long id){
        String result = "success";
        TAdminSys sysUser = AdminSecurityManager.getSessionTAdminSys();

        TCmsHome cmsHome = new TCmsHome();
        try {
            cmsHome = cmsHomeService.findCmsHome(id);
            cmsHome.setUpdateId(sysUser.getId());
            cmsHomeService.inValidCmsHome(cmsHome);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }

}
